package top.lyc829.aircontroller.ui.videoCache;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;


import java.util.ArrayList;
import java.util.List;

import top.lyc829.aircontroller.R;
import top.lyc829.aircontroller.logic.dao.Video;
import top.lyc829.aircontroller.ui.searchVideo.VideoListAdapter;
import top.lyc829.aircontroller.ui.videoCacheDetail.VideoCacheDetailActivity;
import top.lyc829.aircontroller.ui.BaseActivity;

public class VideoCacheActivity extends BaseActivity {

    private ListView listView;
    private List<Video> videoList;
    private VideoListAdapter videoVideoListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_cache);
        init();
        getCacheVideoList();
    }

    private void init(){
        videoList = new ArrayList<>();
        listView = findViewById(R.id.video_list);
        videoVideoListAdapter = new VideoListAdapter(this,R.layout.video_list_item,videoList);
        listView.setOnItemClickListener(adapterListener);
        listView.setAdapter(videoVideoListAdapter);
    }

    private void getCacheVideoList(){
        doGet("getCacheVideoList",Video.class);
    }

    private AdapterView.OnItemClickListener adapterListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Video video = videoList.get(position);
            Intent intent = new Intent(getApplicationContext(), VideoCacheDetailActivity.class);
            intent.putExtra("name",video.getName());
            intent.putExtra("parts",video.getParts());
            startActivity(intent);
        }
    };

    @Override
    protected void netCallBack(Object data) {
        ArrayList<Video> videos = (ArrayList<Video>) data;
        videoList.clear();
        videoList.addAll(videos);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                videoVideoListAdapter.notifyDataSetChanged();
            }
        });
    }
}